package com.mtiiot.fstcpserver.ftp;

import com.mtiiot.fstcpserver.base.BaseService;
import com.mtiiot.fstcpserver.utils.Constants;
import com.mtiiot.fscore.bean.FtpInfo;
import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.bean.elasticsearch.metadata.FtpMetadata;
import com.mtiiot.fscore.inter1.FtpService1;
import com.mtiiot.fscore.utils.RequestType;
import com.mtiiot.fscore.utils.StringUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 16-8-18.
 */
@Transactional
public class FtpServiceImpl1 extends BaseService implements FtpService1 {
    private static final Logger logger = LogManager.getLogger(FtpServiceImpl1.class);
    private static final String REDIS_MQ_MODAL="list";//list or pubAndSub
    //private static final String REDIS_MQ_MODAL="pubAndSub";//list or pubAndSub

    /**
     * ftp连接基本信息
     *
     * @param metadataInfo
     * @param ftpInfo
     * @param content        文件内容
     * @param remoteFileName 存入ftp的文件名
     */
    @Override
    public void upload(MetadataInfo metadataInfo, FtpInfo ftpInfo, String content, String remoteFileName) {
        try {
            //上传文件
            checkMetadata(metadataInfo);//验证元数据
            ObjectMapper mapper = new ObjectMapper();
            String uuid=StringUtil.getUUID();


            //元数据入redis队列
            FtpMetadata md = new FtpMetadata();
            setMetadataBeanDataAndId(metadataInfo, md, RequestType.INSERT.getName(),uuid);
            md.setFtpHost(ftpInfo.getHost());
            md.setFtpPort(ftpInfo.getPort());
            md.setFtpUserName(ftpInfo.getUserName());
            md.setFtpPassword(ftpInfo.getPassword());
            md.setRemoteFileName(remoteFileName);
            metadataInQueue(Constants.TOPIC_METADATAINFO,Constants.TYPE_METADATA_FTP,md);


            //ftp信息入队列
            FtpBean fb = new FtpBean();
            fb.setHost(ftpInfo.getHost());
            fb.setPort(ftpInfo.getPort());
            fb.setUserName(ftpInfo.getUserName());
            fb.setPassword(ftpInfo.getPassword());
            fb.setContent(content);
            fb.setFiles(null);
            fb.setRemoteFileName(remoteFileName);
            fb.setRemoteFilePath(null);
            fb.setReTryTime(0);
            fb.setMetaDataId(uuid);
            String json=mapper.writeValueAsString(fb);
            if(REDIS_MQ_MODAL.equals("list")){
                getJedisCluster().lpush(randomGetFtpRedisKey(),json);
            }else{
                getJedisCluster().publish(Constants.REDIS_PUBSUB_CHANNEL_FTP_001,json);
            }
        } catch (Exception e) {
            //3.异常信息,元数据入redis队列
            handleException(Constants.MODULE_FTP, "FTPServiceImpl1", "upload1", e,logger);
        }
    }

    /**
     * ftp连接基本信息
     *
     * @param metadataInfo
     * @param ftpInfo
     * @param content        文件内容
     * @param remoteFilePath 存入ftp的文件路径
     * @param remoteFileName 存入ftp的文件名
     */
    @Override
    public void upload(MetadataInfo metadataInfo, FtpInfo ftpInfo, String content, String remoteFilePath, String remoteFileName) {
        try {
            //上传文件
            checkMetadata(metadataInfo);//验证元数据
            ObjectMapper mapper = new ObjectMapper();
            String uuid=StringUtil.getUUID();

            //元数据入redis队列
            FtpMetadata md = new FtpMetadata();
            setMetadataBeanDataAndId(metadataInfo,md, RequestType.INSERT.getName(),uuid);
            md.setFtpHost(ftpInfo.getHost());
            md.setFtpPort(ftpInfo.getPort());
            md.setFtpUserName(ftpInfo.getUserName());
            md.setFtpPassword(ftpInfo.getPassword());
            md.setRemoteFileName(remoteFileName);
            md.setRemoteFilePath(remoteFilePath);
            metadataInQueue(Constants.TOPIC_METADATAINFO, Constants.TYPE_METADATA_FTP, md);

            //ftp信息入队列
            FtpBean fb = new FtpBean();
            fb.setHost(ftpInfo.getHost());
            fb.setPort(ftpInfo.getPort());
            fb.setUserName(ftpInfo.getUserName());
            fb.setPassword(ftpInfo.getPassword());
            fb.setContent(content);
            fb.setFiles(null);
            fb.setRemoteFileName(remoteFileName);
            fb.setRemoteFilePath(remoteFilePath);
            fb.setReTryTime(0);
            fb.setMetaDataId(uuid);
            String json=mapper.writeValueAsString(fb);
            if(REDIS_MQ_MODAL.equals("list")){
                getJedisCluster().lpush(randomGetFtpRedisKey(),json);
            }else{
                getJedisCluster().publish(Constants.REDIS_PUBSUB_CHANNEL_FTP_001,json);
            }

        } catch (Exception e) {
            //3.异常信息,元数据入redis队列
            handleException(Constants.MODULE_FTP, "FTPServiceImpl1", "upload2", e,logger);
        }
    }

    /**
     * ftp连接基本信息
     *
     * @param metadataInfo
     * @param ftpInfo
     * @param files          文件内容字节数组
     * @param remoteFileName 存入ftp的文件名
     */
    @Override
    public void upload(MetadataInfo metadataInfo, FtpInfo ftpInfo, byte[] files, String remoteFileName) {
        try {
            //上传文件
            checkMetadata(metadataInfo);//验证元数据
            ObjectMapper mapper = new ObjectMapper();
            String uuid=StringUtil.getUUID();

            //元数据入redis队列
            FtpMetadata md = new FtpMetadata();
            setMetadataBeanDataAndId(metadataInfo,md, RequestType.INSERT.getName(),uuid);
            md.setFtpHost(ftpInfo.getHost());
            md.setFtpPort(ftpInfo.getPort());
            md.setFtpUserName(ftpInfo.getUserName());
            md.setFtpPassword(ftpInfo.getPassword());
            md.setRemoteFileName(remoteFileName);
            metadataInQueue(Constants.TOPIC_METADATAINFO,Constants.TYPE_METADATA_FTP,md);

            //ftp信息入队列
            FtpBean fb = new FtpBean();
            fb.setHost(ftpInfo.getHost());
            fb.setPort(ftpInfo.getPort());
            fb.setUserName(ftpInfo.getUserName());
            fb.setPassword(ftpInfo.getPassword());
            fb.setContent(null);
            fb.setFiles(files);
            fb.setRemoteFileName(remoteFileName);
            fb.setRemoteFilePath(null);
            fb.setReTryTime(0);
            fb.setMetaDataId(uuid);
            String json=mapper.writeValueAsString(fb);
            if(REDIS_MQ_MODAL.equals("list")){
                getJedisCluster().lpush(randomGetFtpRedisKey(),json);
            }else{
                getJedisCluster().publish(Constants.REDIS_PUBSUB_CHANNEL_FTP_001,json);

            }
        } catch (Exception e) {
            //3.异常信息,元数据入redis队列
            handleException(Constants.MODULE_FTP, "FTPServiceImpl1", "upload3", e,logger);
        }
    }

    /**
     * ftp连接基本信息
     *
     * @param metadataInfo
     * @param ftpInfo
     * @param files          文件内容字节数组
     * @param remoteFilePath 存入ftp的文件路径
     * @param remoteFileName 存入ftp的文件名
     */
    @Override
    public void upload(MetadataInfo metadataInfo, FtpInfo ftpInfo, byte[] files, String remoteFilePath, String remoteFileName) {
        try {
            //上传文件
            checkMetadata(metadataInfo);//验证元数据
            ObjectMapper mapper = new ObjectMapper();
            String uuid=StringUtil.getUUID();

            //元数据入redis队列
            FtpMetadata md = new FtpMetadata();
            setMetadataBeanDataAndId(metadataInfo,md, RequestType.INSERT.getName(),uuid);
            md.setFtpHost(ftpInfo.getHost());
            md.setFtpPort(ftpInfo.getPort());
            md.setFtpUserName(ftpInfo.getUserName());
            md.setFtpPassword(ftpInfo.getPassword());
            md.setRemoteFileName(remoteFileName);
            md.setRemoteFilePath(remoteFilePath);
            metadataInQueue(Constants.TOPIC_METADATAINFO, Constants.TYPE_METADATA_FTP, md);

            //ftp信息入队列
            FtpBean fb = new FtpBean();
            fb.setHost(ftpInfo.getHost());
            fb.setPort(ftpInfo.getPort());
            fb.setUserName(ftpInfo.getUserName());
            fb.setPassword(ftpInfo.getPassword());
            fb.setContent(null);
            fb.setFiles(files);
            fb.setRemoteFileName(remoteFileName);
            fb.setRemoteFilePath(remoteFilePath);
            fb.setReTryTime(0);
            fb.setMetaDataId(uuid);
            String json=mapper.writeValueAsString(fb);
            if(REDIS_MQ_MODAL.equals("list")){
                getJedisCluster().lpush(randomGetFtpRedisKey(),json);
            }else{
                getJedisCluster().publish(Constants.REDIS_PUBSUB_CHANNEL_FTP_001,json);
            }

        } catch (Exception e) {
            //3.异常信息,元数据入redis队列
            handleException(Constants.MODULE_FTP, "FTPServiceImpl1", "upload4", e,logger);
        }
    }

    /**
     * 随机获取redis队列值
     * @return
     */
    private String randomGetFtpRedisKey(){
        int max=getFsConfig().getUseFtpRedisKeyCount();
        int x=1+(int)(Math.random()*max);
        switch (x){
            case 1:
                return Constants.FTP_REDISKEY_001;
            case 2:
                return Constants.FTP_REDISKEY_002;
            case 3:
                return Constants.FTP_REDISKEY_003;
            case 4:
                return Constants.FTP_REDISKEY_004;
            case 5:
                return Constants.FTP_REDISKEY_005;
            case 6:
                return Constants.FTP_REDISKEY_006;
            case 7:
                return Constants.FTP_REDISKEY_007;
            case 8:
                return Constants.FTP_REDISKEY_008;
            case 9:
                return Constants.FTP_REDISKEY_009;
            case 10:
                return Constants.FTP_REDISKEY_0010;
            default:
                return Constants.FTP_REDISKEY_001;
        }
    }

}
